home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 5 / dates.zip / DATES.PAS < prev   
Pascal/Delphi Source File  |  1987-10-27  |  5KB  |  198 lines

  1. {$R-,S+,I-,D-,T-,F-,V-,B-,N-}
  2.  
  3. unit Dates;
  4.  
  5. { A unit providing Julian day numbers and date manipulations.
  6.  
  7.   Version 1.00 - 10/26/1987 - First general release
  8.  
  9.   Scott Bussinger
  10.   Professional Practice Systems
  11.   110 South 131st Street
  12.   Tacoma, WA  98444
  13.   (206)531-8944
  14.   Compuserve 72247,2671 }
  15.  
  16.  
  17. interface
  18.  
  19. const BlankDate = $FFFF;                         { Constant for Not-a-real-Date }
  20.  
  21. type Date = Word;
  22.      Day = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
  23.  
  24. function ValidDate(Day,Month,Year: integer): boolean;
  25.   { Check if the day,month,year is a real date storable in a Date variable }
  26.  
  27. procedure DMYtoDate(Day,Month,Year: integer;var Julian: Date);
  28.   { Convert from day,month,year to a date }
  29.  
  30. procedure DateToDMY(Julian: Date;var Day,Month,Year: integer);
  31.   { Convert from a date to day,month,year }
  32.  
  33. function BumpDate(Julian: Date;Days,Months,Years: integer): Date;
  34.   { Add (or subtract) the number of days, months, and years to a date }
  35.  
  36. function DayOfWeek(Julian: Date): Day;
  37.   { Return the day of the week for the date }
  38.  
  39. function DayString(WeekDay: Day): string;
  40.   { Return a string version of a day of the week }
  41.  
  42. function MonthString(Month: integer): string;
  43.   { Return a string version of a month }
  44.  
  45. function DateToStr(Julian: Date): string;
  46.   { Convert a date to a sortable string }
  47.  
  48. function StrToDate(StrVar: string): Date;
  49.   { Convert a sortable string form to a date }
  50.  
  51.  
  52. implementation
  53.  
  54. function ValidDate(Day,Month,Year: integer): boolean;
  55.   { Check if the day,month,year is a real date storable in a Date variable }
  56.   begin
  57.   if (Day<1) or (Year<1900) or (Year>2078)
  58.    then
  59.     ValidDate := false
  60.    else
  61.     case Month of
  62.       1,3,5,7,8,10,12: ValidDate := Day <= 31;
  63.       4,6,9,11: ValidDate := Day <= 30;
  64.       2: ValidDate := Day <= 28 + ord((Year mod 4)=0)*ord(Year<>1900)
  65.       else ValidDate := false
  66.       end
  67.   end;
  68.  
  69. procedure DMYtoDate(Day,Month,Year: integer;var Julian: Date);
  70.   { Convert from day,month,year to a date }
  71.   { Stored as number of days since January 1, 1900 }
  72.   { Note that no error checking takes place in this routine -- use ValidDate }
  73.   begin
  74.   if (Year=1900) and (Month<3)
  75.    then
  76.     if Month = 1
  77.      then
  78.       Julian := pred(Day)
  79.      else
  80.       Julian := Day + 30
  81.    else
  82.     begin
  83.     if Month > 2
  84.      then
  85.       dec(Month,3)
  86.      else
  87.       begin
  88.       inc(Month,9);
  89.       dec(Year)
  90.       end;
  91.     dec(Year,1900);
  92.     Julian := (1461*longint(Year) div 4) + ((153*Month+2) div 5) + Day + 58
  93.     end
  94.   end;
  95.  
  96. procedure DateToDMY(Julian: Date;var Day,Month,Year: integer);
  97.   { Convert from a date to day,month,year }
  98.   var LongTemp: longint;
  99.       Temp: integer;
  100.   begin
  101.   if Julian <= 58
  102.    then
  103.     begin
  104.     Year := 1900;
  105.     if Julian <= 30
  106.      then
  107.       begin
  108.       Month := 1;
  109.       Day := succ(Julian)
  110.       end
  111.      else
  112.       begin
  113.       Month := 2;
  114.       Day := Julian - 30
  115.       end
  116.     end
  117.    else
  118.     begin
  119.     LongTemp := 4*longint(Julian) - 233;
  120.     Year := LongTemp div 1461;
  121.     Temp := LongTemp mod 1461 div 4 * 5 + 2;
  122.     Month := Temp div 153;
  123.     Day := Temp mod 153 div 5 + 1;
  124.     inc(Year,1900);
  125.     if Month < 10
  126.      then
  127.       inc(Month,3)
  128.      else
  129.       begin
  130.       dec(Month,9);
  131.       inc(Year)
  132.       end
  133.     end
  134.   end;
  135.  
  136. function BumpDate(Julian: Date;Days,Months,Years: integer): Date;
  137.   { Add (or subtract) the number of days, months, and years to a date }
  138.   { Note that months and years are added first before days }
  139.   { Note further that there are no overflow/underflow checks }
  140.   var Day: integer;
  141.       Month: integer;
  142.       Year: integer;
  143.   begin
  144.   DateToDMY(Julian,Day,Month,Year);
  145.   Month := Month + Months - 1;
  146.   Year := Year + Years + (Month div 12) - ord(Month<0);
  147.   Month := (Month + 12000) mod 12 + 1;
  148.   DMYtoDate(Day,Month,Year,Julian);
  149.   BumpDate := Julian + Days
  150.   end;
  151.  
  152. function DayOfWeek(Julian: Date): Day;
  153.   { Return the day of the week for the date }
  154.   begin
  155.   DayOfWeek := Day(succ(Julian) mod 7)
  156.   end;
  157.  
  158. function DayString(WeekDay: Day): string;
  159.   { Return a string version of a day of the week }
  160.   const DayStr: array[Sunday..Saturday] of string[9] =
  161.           ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  162.   begin
  163.   DayString := DayStr[WeekDay]
  164.   end;
  165.  
  166. function MonthString(Month: integer): string;
  167.   { Return a string version of a month }
  168.   const MonthStr: array[1..12] of string[9] =
  169.           ('January','February','March','April','May','June','July','August','September','October','November','December');
  170.   begin
  171.   MonthString := MonthStr[Month]
  172.   end;
  173.  
  174. function DateToStr(Julian: Date): string;
  175.   { Convert a date to a sortable string }
  176.   const Result: record
  177.           case integer of
  178.             0: (Len: byte;
  179.                 W: word);
  180.             1: (Str: string[2])
  181.           end = (Str:'  ');
  182.   begin
  183.   Result.W := swap(Julian);
  184.   DateToStr := Result.Str
  185.   end;
  186.  
  187. function StrToDate(StrVar: string): Date;
  188.   { Convert a sortable string form to a date }
  189.   var Temp: record
  190.         Len: byte;
  191.         W: word
  192.         end absolute StrVar;
  193.   begin
  194.   StrToDate := swap(Temp.W)
  195.   end;
  196.  
  197. end.
  198.